_weaponclass = _this select 0;
_helo = _this select 1;
_helocallsign = _this select 2;
_type = _this select 3;
_class = typeof _helo;

// SFS fire duration
_CanonShort = 45;
_CanonMedium = 70;
_CanonLong = 120;

// Default fire rate - set to Apache standards - 600 rounds per min
_firerate = _CanonShort;

// SFS fire rate types
// higher value = lower rate of fire; lower value = higher rate of fire. Adjust this with the firerate for desired results.
_rof = 0.042;

player sidechat format["Helo's is: %1 %2 %3 %4 %5",_helo,_weaponclass,_helocallsign,_type,_class];

	if ((_class == "AH1Z") && (_type == "SFS"))then{
		_rof = .022;
		_firerate = _CanonLong;
		// hint format["changed ROF to:  %1\n%2\n%3", _rof, _class, _firerate];
		// sleep 5;
	};

	["callfire",[player,driver _helo,_helocallsign,_type]] execVM (RUM_PATH+"AHel\radiodialogs.sqf");
	sleep (3 + random 3);

	_search = false;

	private ["_laser","_tar"];

	if(0 == _helo ammo _weaponclass)exitWith{_pilot sidechat "Winchester";};

	for [{_n=0},{_n<30},{_n=_n+1}] do {
		if((count (screenToWorld [0.5,0.5] nearObjects ["LaserTargetBase",100]))>0)then{
			_laser = screenToWorld [0.5,0.5] nearObjects ["LaserTargetBase",100];
			_tar = _laser select 0;
			_n = 30;
		};
		sleep 1;
		// hintsilent format ["%1 , %2 ,%3, %4",_tar, _n, player, _rof];
		if (_n > 15) then {
			hintsilent "Searching for Laser Target\n\nIs the laser on?";
		}else{
			hintsilent "Searching for Laser Target";
		};
	};

	_pilot = driver _helo;
	_grp = group _pilot;

	RUM_AHelBobUpLocal = {

			// make the helo bob up to fire on target.
			_helo domove getpos _helo;
			_wpAH=_grp addWaypoint [_helo, 0];
			_wpAH setWaypointType "MOVE";
			_wpAH setWaypointSpeed "LIMITED";
			_wpAH setWaypointBehaviour "CARELESS";
			_wpAH setWaypointCombatMode "BLUE";
			_wpAH setWaypointCompletionRadius 0;
			_helo flyInheight RUM_AHEL_flyFireHeight;

			waituntil{((getPos _helo select 2) >= (RUM_AHEL_flyFireHeight - 6.5))};

	};
	RUM_AHelBobDwnLocal = {

			// make the helo bob up to fire on target.
			_helo doMove [ (getPos _helo select 0), (getPos _helo select 1), RUM_AHEL_flyStationHeight];
			_wpAH=_grp addWaypoint [_helo, 0];
			_wpAH setWaypointType "MOVE";
			_wpAH setWaypointSpeed "LIMITED";
			_wpAH setWaypointBehaviour "CARELESS";
			_wpAH setWaypointCombatMode "BLUE";
			_wpAH setWaypointCompletionRadius 0;
			_helo flyInHeight RUM_AHEL_flyStationHeight;

	};
	RUM_AHelFireCanonFn = {
		
			objDone = true;
			while {objDone} do {
				_helo fireAtTarget [_tar,_weaponclass];
					waitUntil {_firerate = _firerate - 1; sleep _rof; _firerate < 0 || objDone};
						if (_firerate < 0) then {
							objDone = false;
						};
			};

	};
	RUM_AHelFireMissileFn = {
		
			_bool = _helo fireAtTarget [_tar,_weaponclass];
			waitUntil{_bool};
			_pilot sidechat "Missile Away";

	};

	
if(count _laser == 0)then{
	_pilot sidechat "No Joy";
}else{
	hintsilent "Target Located\n\nStandby...";
	sleep 1;
	_dir = ((getpos _tar select 0)-(getpos _helo select 0)) atan2 ((getpos _tar select 1)-(getpos _helo select 1));
	_dir = 10 * round (_dir/10);
	sleep .5;
	_pilot sidechat "Target Located";
	_helodir = 10 * round ((getdir _helo)/10);
	while {!(_dir == _helodir)} do {
		private "_dir2";
		if(_dir < _helodir)then{_dir2 = _helodir - 2.5}else{_dir2 = _helodir + 2.5};
		_helodir = _dir2;
		_helo setdir _dir2;
		sleep 0.02;	
	};
		_pilot sidechat "Target Aquired";

	//check distance from helo to target
		_targetpos = [getPos _tar select 0,getPos _tar select 1,0];
		_targetdistance = _targetpos distance [getPos _helo select 0, getPos _helo select 1, 0];
		// hint format ["%1 degrees, %2 meters away",_dir, _targetdistance];
		hintsilent format ["Target Distance: %1",_targetdistance];
		_pilot sidechat format ["%2 meters away",_dir, _targetdistance];

	//determine if the target is to far away for guns.

	if((_targetdistance >= 1200) && (_type == "SFS") || (_targetdistance <= 175) && (_type == "SFS"))then{
		// hint format["Target is outside effective range. %1",_targetdistance];
		// _pilot sidechat format["Target is outside effective range. %1",_targetdistance];
		hintsilent format["Operation Cancelled.\n\n Target is either outside effective range or too close.\n\n Target distance: %1",_targetdistance];
		_pilot sidechat format["Operation Cancelled. Target is either outside effective range or too close. Target distance: %1 meters",(round _targetdistance)];
		sleep 1.5;
		exit;
	}else{

			//check to see if the target it in the ROE Zone
			//if!([_tar] call ROEzone)exitwith{_pilot sidechat "Target In Restricted Zone";};

		
		switch (_type) do {

		//create variable switched weapons for different firing types
		//=================================== Canon Fire ==========================================//
				case "SFS":{
				//fires canon at the the lased target - works - firerate is set at the top of this file.
				
				// _pilot sidechat "SFS Section";
				
					// If (local _helo) then {

						// hint "local command being sent";
						// sleep 2;

						//this is for local and local spawned unit control
						call RUM_AHelBobUpLocal;

						// hintsilent format ["Fire Rate: %1,\nTarget: %2,\nWeapon Type: %3", _firerate, _tar, _weaponclass];
						_pilot sidechat "Firing Canon";

						waituntil{((getPos _helo select 2) >= (RUM_AHEL_flyFireHeight - 6.5))};
						
						call RUM_AHelFireCanonFn;
						
					// }else{

						// hint "server command being sent"; 
						// sleep 2;

						//this is for server and server spawned unit control
						// _mtype = "MoveBobUp";
						// RUM_AHel_ServerMoveStation = [_grp,_pos,_mtype,_helo,_helodir,RUM_AHEL_flyFireHeight];
						// publicVariable "RUM_AHel_ServerMoveStation";

						// waituntil{((getPos _helo select 2) >= (RUM_AHEL_flyFireHeight - 6.5))};

						//this is for server and server spawned unit control
						// RUM_AHel_ServerGunFire = [_tar,_firerate,_type,_helo,_weaponclass,RUM_AHEL_flyFireHeight,_helodir,_rof];
						// publicVariable "RUM_AHel_ServerGunFire";

					// };
				};

		//=================================== Hellfire Missile ===================================//
				case "ATGM":{

				// _pilot sidechat "ATGM Section";

					// If (local _helo) then {

						// hint "local command being sent";
						// sleep 2;

						//this is for local and local spawned unit control
						call RUM_AHelBobUpLocal;

						// hintsilent format ["Fire Rate: %1,\nTarget: %2,\nWeapon Type: %3", _firerate, _tar, _weaponclass];
						_pilot sidechat "Firing Missile";

						waituntil{((getPos _helo select 2) >= (RUM_AHEL_flyFireHeight - 6.5))};

						call RUM_AHelFireMissileFn;

					// }else{

						// hint "server command being sent"; 
						// sleep 2;

						//this is for server and server spawned unit control
						// _mtype = "MoveBobUp";
						// RUM_AHel_ServerMoveStation = [_grp,_pos,_mtype,_helo,_helodir,RUM_AHEL_flyFireHeight];
						// publicVariable "RUM_AHel_ServerMoveStation";

						waituntil{((getPos _helo select 2) >= (RUM_AHEL_flyFireHeight - 6.5))};

						//this is for server and server spawned unit control
						// RUM_AHel_ServerGunFire = [_tar,_firerate,_type,_helo,_weaponclass,RUM_AHEL_flyFireHeight,_helodir];
						// publicVariable "RUM_AHel_ServerGunFire";

					// };

				};	

		//=================================== FFAR Rocket Barrage ================================//
				case "FFAR":{
				
				};	
			};

		If (local _helo) then {
			
			//this is for local and local spawned unit control
			call RUM_AHelBobDwnLocal;

		}else{

			//this is for server and server spawned unit control
			// _pilot sidechat"Server";
		
			_mtype = "MoveBobDown";
			RUM_AHel_ServerMoveStation = [_grp,_pos,_mtype,_helo];
			publicVariable "RUM_AHel_ServerMoveStation";

		};
	};
};
if(true)exitwith{};

